//
//  HXWelcomeViewController.swift
//  DemoSinaWeibo
//
//  Created by Mon on 15/11/30.
//  Copyright © 2015年 Mon. All rights reserved.
//

import UIKit

import SDWebImage

class HXWelcomeViewController: UIViewController {
    
    // MARK: - 属性
    private var iconViewBottomConstrain: NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()
        // 先准备好UI,在进行修改
        prepareUI()
        
//        //显示当前登录用户在欢迎界面的头像
//            //获取头像 --> 可选绑定
//        if let avatar_large = HXUserAccount.loadUserAccount()?.avatar_large {
//            iconView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage:UIImage(named: "avatar_default_big"))
//        }
        
        /// 加载用户头像
        if let loadIcon = HXUserAccount.loadUserAccount()?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: loadIcon), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        /// 加载用户数据
        HXUserAccount.loadUserAccount()?.loadUserInfo()
    }
    
    override func viewDidAppear(animated: Bool) {
        // 方法调用的位置不同,会导致动画的效果不同
            // 在viewDidLoad中实现的话,会让整个控件从左到右出来
        moveAnimation()
    }
    
    //头像动画 --> 改变动画,因为使用autoLayout,故改变的应该是视图的约束constrain的值
    func moveAnimation() {
        iconViewBottomConstrain?.constant = -500
        
        // 头像动画 --> 使用UIView的动画
        /*
        usingSpringWithDamping --> 弹簧效果 0~1
        initialSpringVelocity --> 加速度
        */
        UIView.animateWithDuration(1.5, delay: 0.1, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                print("欢迎动画完毕")
                
//                (UIApplication.sharedApplication().delegate as? AppDelegate)?.switchController(true)
                AppDelegate.switchRootController(true)
        }
    }
    
    // 准备UI
    func prepareUI() {
        view.addSubview(backGroundView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        //取消系统约束
        backGroundView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        //添加约束
        /*
        使用VFL约束
        H:表示水平约束
        V:表示垂直约束
        []:控件名
        |:父控件的间距
        */
        /// 背景
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bkg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkg": backGroundView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bkg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkg": backGroundView]))
        
        /// 头像
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
            // 记录约束
        iconViewBottomConstrain = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(iconViewBottomConstrain!)
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 85))
        
        /// 欢迎label
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
    }
    
    // MARK: - 懒加载欢迎视图控件
    /// 背景
    private lazy var backGroundView = UIImageView(image: UIImage(named: "ad_background"))
    
    /// 头像
    private lazy var iconView: UIImageView = {
        let iconV = UIImageView(image: UIImage(named: "avatar_default_big"))
        //设置头像圆角和半径
        iconV.layer.cornerRadius = 42.5 // 半径
        iconV.layer.masksToBounds = true // 切角
        return iconV
    }()
    
    /// 欢迎label
    private lazy var welcomeLabel: UILabel = {
        let wcl = UILabel()
        //设置label属性
        wcl.text = "Welcome You Back"
        wcl.font = UIFont.systemFontOfSize(13)
        wcl.sizeToFit()
        return wcl
    }()

}
